package cn.edu.zafu.bigdata.XuShuXin.Exp1203;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
 * @author: aluvfy
 * @description:
 * @date: 2024/12/3 10:28
 */
public class UDPClient_time_xsx {
    public static void main(String[] args) throws Exception {
        String serverHostname = "localhost"; //服务器的主机名或IP地址，可以用来交互不同的机子
        int port = 12345; // 服务端监听的端口号

        DatagramSocket clientSocket = new DatagramSocket();
        InetAddress IPAddress = InetAddress.getByName(serverHostname);
        byte[] sendData = "Time".getBytes();
        byte[] receiveData = new byte[1024];

        //打包数据包，发送到服务端
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
        clientSocket.send(sendPacket);

        //接受服务端的响应，打印出来
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        String modifiedSentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
        System.out.println("Current time from server: " + modifiedSentence);

        clientSocket.close();
    }
}
